WIDTH = 800
HEIGHT = 600


-- initialize
object_list = {}

paused = false
moving = true
rotating = true


--display:create ( "Benchmark Demo", WIDTH, HEIGHT, 32, true )

local dm = DisplayMode ( )
dm.width = WIDTH
dm.height = HEIGHT
dm.depth = 32
dm.windowed = true
display:set_mode ( dm )

-- help
font = Font ( )
font:load_system ( "Courier New", 12 )
sprite = Sprite ( )
display.viewport:add_child ( sprite )
sprite.depth = -10000
sprite.canvas:set_font ( font )

help = true

htimer = Timer ( )
htimer:start ( 1, true )
htimer.on_tick = function ( htimer )
	local lh = font:get_height ( )
	local x = -WIDTH / 2 + ( lh - font:get_size ( ) )
	local y = HEIGHT / 2
	local fps = 1000 / htimer:get_delta ( )
	local c = sprite.canvas
	c:clear ( )
	if help == false then
		return
	end
	c:move_to ( x, y - lh )
	c:write ( "Moving and rotating a given number of sprites" )
	c:move_to ( x, y - lh * 2 )
	c:write ( "FPS - " .. fps )
	c:move_to ( x, y - lh * 3 )
	c:write ( "Sprites - " .. #object_list )
	c:move_to ( x, y - lh * 4 )
	c:write ( "KEY_UP - add sprites" )
	c:move_to ( x, y - lh * 5 )
	c:write ( "KEY_DOWN - remove sprites" )
	c:move_to ( x, y - lh * 6 )
	c:write ( "KEY_1 - toggle update" )
	c:move_to ( x, y - lh * 7 )
	c:write ( "KEY_2 - toggle movement" )
	c:move_to ( x, y - lh * 8 )
	c:write ( "KEY_3 - toggle rotation" )
	c:move_to ( x, y - lh * 9 )
	c:write ( "KEY_F1 - toggle help" )
end


-- enable disable the different test
if keyboard then
	keyboard.on_press = function ( keyboard, k )
		if k == KEY_1 then
			paused = not paused
		elseif k == KEY_2 then
			moving = not moving
		elseif k == KEY_3 then
			rotating = not rotating
		elseif k == KEY_F1 then
			help = not help
		end
	end
end

-- update
timer = Timer ( 1, true )
timer:start ( )
timer.on_tick = function ( timer )

	local elapsed = timer:get_delta ( )

	local delta = elapsed / 1000 * 20

	-- do not iterate if paused
	if paused == false then
		for k, v in ipairs ( object_list ) do

			-- motion
			if moving == true then
				v.x = v.x + v.inc_x * delta
				v.y = v.y + v.inc_y * delta

				local x = v.x
				local y = v.y

				v.s:change_position ( v.inc_x * delta, v.inc_y * delta )

				if x < -WIDTH / 2 or x > WIDTH / 2 then
					v.inc_x = -v.inc_x
				end
				
				if y < -HEIGHT / 2 or y > HEIGHT / 2 then
					v.inc_y = -v.inc_y
				end

			end

			-- rotation
			if rotating == true then
				v.s:change_rotation ( v.inc_rot * delta )
			end
		end
	end

	-- create 5 new objects
	if keyboard and keyboard:is_down ( KEY_UP ) then
		for i = 0, 20 do
			local s = {}
			s.s = Sprite ( )

			s.x = math.random ( 0, WIDTH ) - WIDTH / 2
			s.y = math.random ( 0, HEIGHT ) - HEIGHT / 2

			s.s:change_position ( s.x, s.y )

			-- size
			s.w = math.random ( 10, 20 )
			s.h = math.random ( 10, 20 )

			-- create object
			s.s.canvas:rectangle ( s.w, s.h )
			s.s.canvas:set_fill_style ( Color( math.random(255), math.random(255), math.random(255) ), 1 )
			s.s.canvas:fill ( )

			s.s.depth = #object_list
			display.viewport:add_child ( s.s )

			-- motion
			s.inc_x = math.random ( -100, 100 ) / 100
			s.inc_y = math.random ( 100, 100 ) / 100

			-- rotation
			s.inc_rot = math.random ( -5, 5 )

			table.insert ( object_list, s )
		end
	end

	-- release 5 existing objects
	if keyboard and keyboard:is_down ( KEY_DOWN ) then
		local diff = 20
		if #object_list < diff then
			diff = #object_list - 1
		end
		for i = 0, diff do
			local s = table.remove ( object_list )
			display.viewport:remove_child ( s.s )
		end 
	end
end

